﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmSelfLearning : MeForm
    {
        List<Record> records;
        const int pageSize = 20;
        int pageIndex;
        int MaxPage;
           
        public frmSelfLearning()
        {
            InitializeComponent();
            this.dataGridView.AutoGenerateColumns = false;
            MaxPage = DataFactory.ChatRecords.Records.Count / pageSize;
            if (DataFactory.ChatRecords.Records.Count % pageSize > 0)
            {
                MaxPage += 1;
            }
            BindData(0);
        }

        void BindData(int pageIndex)
        {
            this.pageIndex = pageIndex;

            records = DataFactory.ChatRecords.Records.Skip((pageSize * pageIndex)).Take(pageSize).ToList();


            foreach (var dd in records)
            {
                if (!string.IsNullOrEmpty(UtilHelper.FindEqualMessages(dd.Text)))
                {
                    dd.IsAddMessage = "已加入";
                }
            }
            this.dataGridView.DataSource = records;
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                Record record = this.dataGridView.CurrentRow.DataBoundItem as Record;
                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "add" && record != null)
                {
                    if (!record.IsAddMessage.Equals("已加入"))
                    {
                        DataFactory.Messages.Nodes.Add(new Node() { Text = record.Text, Key = record.Text });
                        record.IsAddMessage = "已加入";
                        dataGridView.Refresh();
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && record != null)
                {
                    DataFactory.ChatRecords.Records.Remove(record);
                    BindData(pageIndex);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (MaxPage > pageIndex)
                BindData(pageIndex+1);
        }

      private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (pageIndex > 0)
                BindData(pageIndex-1);
        }

        private void btnPageIndex_Click(object sender, EventArgs e)
        {
            int index;
            int.TryParse(txtPageIndex.Text, out index);
            if (MaxPage <= index && index > 0)
                BindData(index - 1);
        }

        private void txtPageIndex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < (char)46 || e.KeyChar > (char)57) && (e.KeyChar != (char)8 || e.KeyChar == (char)47))
            {
                e.Handled = true;
            }
            else
                e.Handled = false;
        }

        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            if (MeMessageBox.ShowQuestion("确在全删除自学短语吗?") == DialogResult.Yes)
            {
                DataFactory.ChatRecords.Records.Clear();
                dataGridView.DataSource = null;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "*.jlso|*.jlso";
            save.FileName = "";
            save.ShowDialog();
            string fileName = save.FileName;
            if (!string.IsNullOrEmpty(fileName)) {
                SerializerFactory.SerializeFile<ChatRecords>(DataFactory.ChatRecords, fileName);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.jlso|*.jlso";
            open.FileName = "";
            open.ShowDialog();
            string fileName = open.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                DataFactory.ChatRecords = SerializerFactory.DeserializeFile<ChatRecords>(fileName) as ChatRecords;
                if (DataFactory.ChatRecords != null)
                {
                    BindData(0);
                }
            }

        }
    }
}